package com.controller;

import com.pojo.Address;
import com.pojo.User;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/users")//这里表示这个类中其他的方法访问时都要在其对应的 @RequestMapping网址前加上/user
public class UserControlller {

    @RequestMapping("/save")
    @ResponseBody
    public String save(@RequestBody User user){
        System.out.println("save…… "+user);
        return "{'user':'root'}";
    }

    //接受日期类型
    @RequestMapping("/date")
    @ResponseBody
    public String date(@DateTimeFormat(pattern = "yyyy-MM-dd") Date date){
        System.out.println(date);
        return "{'date':'date'}";
    }
    //设置以JSON数据进行返回
    @RequestMapping("/back")
    @ResponseBody
    public List<User> back(){
        User user=new User();
        user.setName("xiaowu");
        user.setPassword("123456");
        Address address=new Address();
        address.setCountry("china");
        address.setProvince("guangdong");
        user.setAddress(address);
        //
        User user1=new User();
        user1.setName("xiaowu");
        user1.setPassword("123456");
        //
        List<User> users = new ArrayList<>();
        users.add(user);
        users.add(user1);
        return users;
    }


//    @RequestMapping (value ="/{id}", method = RequestMethod.DELETE )
    @DeleteMapping ("/{id}")
    @ResponseBody
    public String  delete(@PathVariable  Integer  id){
        System.out.println("delete"+id );
        return "{user:root}";
    }





}
